package d4_com.itheima.stream;

import java.util.*;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        //1、如何获取List集合的Stream流?
        List<String> names = new ArrayList<>();
        Collections.addAll(names, "张三丰", "张无忌", "周芷若", "赵敏", "张强");
        Stream<String> stream = names.stream();

        // 2、如何获取Set集合的Stream流?
        Set<String> set = new HashSet<>();
        Collections.addAll(set, "刘德华", "张曼玉", "蜘蛛精", "马德", "德玛西亚");
        Stream<String> stream1 = set.stream();
        stream1.filter(s -> s.contains("德")).forEach(s -> System.out.println(s));

        //3、如何获取Map集合的Stream流?
        Map<String, Double> map = new HashMap<>();
        map.put("古力娜扎", 172.3);
        map.put("迪丽热巴", 168.3);
        map.put("马尔扎哈", 166.3);
        map.put("卡尔扎巴", 168.3);

        Set<String> keys = map.keySet();
        Stream<String> ks = keys.stream();
        Collection<Double> values = map.values();
        Stream<Double> vs = values.stream();

        Set<Map.Entry<String, Double>> ksvs = map.entrySet();
        Stream<Map.Entry<String, Double>> stream2 = ksvs.stream();
        stream2.filter(e -> e.getKey().contains("巴")).forEach(e -> System.out.println(e.getKey() + "--->" + e.getValue()));

        //4、如何获取数组的Stream流?
        String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};
        Stream<String> stream3 = Arrays.stream(names2);
        Stream<String> stream4 = Stream.of(names2);

        Stream<String> st1 = Stream.of("张三", "李四");
        Stream<String> st2 = Stream.of("张三2", "李四2", "王五");
        //Stream流的合并
        Stream<String> allSt = Stream.concat(st1, st2);
        allSt.forEach(System.out::println);

        //Stream常用中间方法
        /**
         * filter 数据过滤
         * sorted 对元素进行排序
         * limit 获取前几个元素
         * skip 跳过前几个元素
         * distinct 去除流中重复元素
         * map 对元素进行加工，并返回对应的新流
         * concat 合并a，b两个流为一个流
         */

        //Stream常用终结方法 （终结方法指的是调用完成后，不会返回新Stream了，没法继续使用流了。）
        /**
         * forEach //遍历
         * count 求和
         * max 最大值(需要比较规则)
         * min 最小值(需要比较规则)
         * collect(collectors,tolist()) //把流的数据收集到指定集合里面
         */
    }

}
